package com.ruoyi.project.WebMoudle.WxCitys.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.constant.WxConstant;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.web.domain.Ztree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.WebMoudle.WxCitys.mapper.WxCitysMapper;
import com.ruoyi.project.WebMoudle.WxCitys.domain.WxCitys;
import com.ruoyi.project.WebMoudle.WxCitys.service.IWxCitysService;
import com.ruoyi.common.utils.text.Convert;

/**
 * 城区设定Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-06-18
 */
@Service
public class WxCitysServiceImpl implements IWxCitysService 
{
    @Autowired
    private WxCitysMapper wxCitysMapper;

    /**
     * 查询城区设定
     * 
     * @param cityId 城区设定ID
     * @return 城区设定
     */
    @Override
    public WxCitys selectWxCitysById(Long cityId)
    {
        return wxCitysMapper.selectWxCitysById(cityId);
    }

    /**
     * 查询城区设定列表
     * 
     * @param wxCitys 城区设定
     * @return 城区设定
     */
    @Override
    public List<WxCitys> selectWxCitysList(WxCitys wxCitys)
    {
        return wxCitysMapper.selectWxCitysList(wxCitys);
    }

    /**
     * 新增城区设定
     * 
     * @param wxCitys 城区设定
     * @return 结果
     */
    @Override
    public int insertWxCitys(WxCitys wxCitys)
    {
        WxCitys info = wxCitysMapper.selectWxCitysById(wxCitys.getParentId());
        // 如果父节点不为"正常"状态,则不允许新增子节点
        if (!WxConstant.CITY_NORMAL.equals(info.getStatus()))
        {
            throw new BusinessException("城区禁用停用，不允许新增");
        }
        wxCitys.setCreateBy(ShiroUtils.getLoginName());
        wxCitys.setAncestors(info.getAncestors() + "," + wxCitys.getParentId());
        wxCitys.setCreateTime(DateUtils.getNowDate());
        return wxCitysMapper.insertWxCitys(wxCitys);
    }

    /**
     * 修改城区设定
     * 
     * @param wxCitys 城区设定
     * @return 结果
     */
    @Override
    public int updateWxCitys(WxCitys wxCitys)
    {
        wxCitys.setUpdateTime(DateUtils.getNowDate());
        return wxCitysMapper.updateWxCitys(wxCitys);
    }

    /**
     * 删除城区设定对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteWxCitysByIds(String ids)
    {
        return wxCitysMapper.deleteWxCitysByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除城区设定信息
     * 
     * @param cityId 城区设定ID
     * @return 结果
     */
    @Override
    public int deleteWxCitysById(Long cityId)
    {
        return wxCitysMapper.deleteWxCitysById(cityId);
    }

    /**
     * 校验城区名称是否重复
     * @param wxCitys
     * @return
     */
    @Override
    public String checkNameUnique(WxCitys wxCitys) {
        Long deptId = StringUtils.isNull(wxCitys.getCityId()) ? -1L : wxCitys.getCityId();
        WxCitys info = wxCitysMapper.checkNameUnique(wxCitys.getCityName(), wxCitys.getParentId());
        if (StringUtils.isNotNull(info) && info.getCityId().longValue() != deptId.longValue())
        {
            return WxConstant.CITY_NAME_NOT_UNIQUE;
        }
        return WxConstant.CITY_NAME_UNIQUE;
    }

    /**
     * 查询该城区下级个数
     * @param cityId
     * @return
     */
    @Override
    public int selectCitysCount(Long cityId) {
        return wxCitysMapper.selectCitysCount(cityId);
    }

    /**
     * 加载城区列表树
     */
    @Override
    public List<Ztree> selectWxCitysTree(WxCitys wxCitys) {
        List<WxCitys> wxCitysList = wxCitysMapper.selectWxCitysList(wxCitys);
        List<Ztree> ztrees = initZtree(wxCitysList);
        return ztrees;
    }

    /**
     * 对象转城区树
     * @return 树结构列表
     */
    private List<Ztree> initZtree(List<WxCitys> wxCitysList) {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (WxCitys wxCitys : wxCitysList)
        {
            if (WxConstant.CITY_NORMAL.equals(wxCitys.getStatus()))
            {
                Ztree ztree = new Ztree();
                ztree.setId(wxCitys.getCityId());
                ztree.setpId(wxCitys.getParentId());
                ztree.setName(wxCitys.getCityName());
                ztree.setTitle(wxCitys.getCityName());
                ztrees.add(ztree);
            }
        }
        return ztrees;
    }

    @Override
    public List<WxCitys> selectLastList() {
        return wxCitysMapper.selectLastList();
    }

    @Override
    public List<Long> selectListId(WxCitys wxCitys) {
        return wxCitysMapper.selectListId(wxCitys);
    }

    @Override
    public Integer selectShopCount(Long cityId) {
        return wxCitysMapper.selectShopCount(cityId);
    }

    @Override
    public Long queryCityIdByName(String cityName) {
        return wxCitysMapper.queryCityIdByName(cityName);
    }
}
